﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_MyUserControls_TH_ThemSanPham_Case : System.Web.UI.UserControl
{
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDanhSachNPP();
        }

       
    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    public void LoadDanhSachNPP()
    {
        NhaPhanPhoiBUS objNPP = new NhaPhanPhoiBUS(TGDDConfiguration.DbConnectionString);
        try
        {
            objDataSet = objNPP.SelectNhaPhanPhoiAll();
            for (int i = 0; i < objDataSet.Tables["SelectNhaPhanPhoiAll"].Rows.Count; i++)
            {
                DataRow row = objDataSet.Tables["SelectNhaPhanPhoiAll"].Rows[i];
                ListItem item = new ListItem(row["TenNhaPhanPhoi"].ToString(), row["id"].ToString());
                cbxNhaPhanPhoi.Items.Add(item);
            }
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
    }
    protected void lkbtnThem_Click(object sender, EventArgs e)
    {
        if (fileUploadHinhAnh.FileName.Equals(""))
        {
            MessageBox("Vui lòng chọn ảnh để upload");
            return;
        }
        string loaisp = Request.QueryString["loaisp"];
        string nsx = Request.QueryString["nsx"];

        SanPhamDTO spDto = new SanPhamDTO();
        spDto.TenSanPham = txtTenSP.Text;
        spDto.HinhMinhHoa = fileUploadHinhAnh.FileName;
        spDto.Gia = long.Parse(txtGia.Text);
        spDto.IdLoaiSP = int.Parse(loaisp);
        spDto.IdNSX = int.Parse(nsx);
        spDto.SoLuongTon = int.Parse(txtSoLuongTon.Text);
        spDto.BaoHanh = txtBaoHanh.Text;
        spDto.IdNhaPhanPhoi = int.Parse(cbxNhaPhanPhoi.SelectedItem.Value.ToString());
        spDto.MoTa = editorMoTa.Content;

        SanPhamBUS objSP = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        if (objSP.InsertSanPham(spDto))
        {
            if (!fileUploadHinhAnh.FileName.Equals(""))
            {
                string strBaseLocation = Server.MapPath("..") + "\\images\\products\\" + fileUploadHinhAnh.FileName;

                if (fileUploadHinhAnh.PostedFile != null)
                {
                    fileUploadHinhAnh.PostedFile.SaveAs(strBaseLocation);
                }
            }

            MessageBox("Thêm thành công");
        }
        else
        {
            MessageBox("Thêm thất bại");
        }
    }
}
